package com.up.upzl.biz.controller;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.hash.Hashing;
import com.up.upzl.biz.domain.SysAccount;
import com.up.upzl.biz.domain.SysAccountLoginLog;
import com.up.upzl.biz.domain.SysRole;
import com.up.upzl.biz.service.SysAccountLoginLogService;
import com.up.upzl.biz.service.SysAccountService;
import com.up.upzl.biz.service.SysRoleService;

/**
 * 系统用户控制器
 * 
 * @author geeks
 *
 */
@Controller
@RequestMapping("sysAccount")
public class SysAccountController {

	@Autowired
	private SysAccountService sysAccountService;

	@Autowired
	private SysRoleService sysRoleService;
	
	@Autowired
	private SysAccountLoginLogService sysAccountLoginLogService;

	/**
	 * 列表页
	 */
	@GetMapping("list")
	public String accountList(String name, String account, Pageable page, Model model) {
		page = new PageRequest(page.getPageNumber(), page.getPageSize(), new Sort(Direction.DESC, "id"));
		Page<SysAccount> list = sysAccountService.findByCondition(name, account, page);
		model.addAttribute("pageList", list);
		return "sysAccount/list";
	}

	/**
	 * 跳转到添加页面
	 */
	@GetMapping("addPage")
	public String add(Model model) {
		List<SysRole> roles = sysRoleService.findAll();
		model.addAttribute("roles", roles);
		return "sysAccount/add";
	}

	/**
	 * 保存对象
	 */
	@PostMapping("add")
	public @ResponseBody SysAccount save(SysAccount sysAccount) {
		sysAccount.setPassword(Hashing.sha256().hashString(sysAccount.getPassword(), StandardCharsets.UTF_8).toString());
		return sysAccountService.save(sysAccount);
	}

	/**
	 * 跳转到更新页面
	 */
	@GetMapping({ "updatePage", "detail" })
	public String update(Long id, Model model) {
		SysAccount sysAccount = sysAccountService.findOne(id);
		model.addAttribute("entity", sysAccount);
		List<SysRole> roles = sysRoleService.findAll();
		model.addAttribute("roles", roles);
		return "sysAccount/update";
	}

	/**
	 * 更新
	 */
	@PostMapping("update")
	public @ResponseBody SysAccount update(SysAccount sysAccount) {
		Assert.notNull(sysAccount.getId(), "Id can`t be null");
		SysAccount cursysAccount = sysAccountService.findOne(sysAccount.getId());
		if(sysAccount.getPassword() == null) {
			sysAccount.setPassword(cursysAccount.getPassword());
		}else {
			sysAccount.setPassword(Hashing.sha256().hashString(sysAccount.getPassword(), StandardCharsets.UTF_8).toString());			
		}
		return sysAccountService.save(sysAccount);
	}

	/**
	 * 删除
	 */
	@PostMapping("delete")
	public @ResponseBody Map<String, String> delete(SysAccount sysAccount) {
		Assert.notNull(sysAccount.getId(), "Id can`t be null");
		Map<String, String> map = new HashMap<>();
		try {
			sysAccountService.delete(sysAccount.getId());
		} catch (Exception e) {
			map.put("Type", "500");
			return map;
		}
		map.put("Type", "200");
		return map;
	}
	
	/**
	 * 登陆日志列表
	 * 
	 * @return 视图名称
	 */
	@GetMapping("logList")
	public String logList(Pageable page, Model model) {
		Page<SysAccountLoginLog> list = sysAccountLoginLogService.findByCondition(page);
		model.addAttribute("pageList", list);
		
		return "sysAccount/logList";
	}
	
	/**
	 * 删除登陆日志
	 */
	@PostMapping("logDelete")
	public @ResponseBody Map<String, String> delete(SysAccountLoginLog sysAccountLoginLog) {
		Assert.notNull(sysAccountLoginLog.getId(), "Id can`t be null");
		Map<String, String> map = new HashMap<>();
		try {
			sysAccountLoginLogService.delete(sysAccountLoginLog.getId());
		} catch (Exception e) {
			map.put("Type", "500");
			return map;
		}
		map.put("Type", "200");
		return map;
	}

}
